package com.example.teamtribe.main.mine.sign;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;

public class SignActivity extends BaseActivity implements View.OnClickListener, SignContract.View {

    private TextView tvSaying;
    private TextView tvDays;
    private Button btSign;
    private ProgressBar progressBar;
    private SignContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        tvSaying = findViewById(R.id.tv_saying);
        tvDays = findViewById(R.id.tv_days);
        btSign = findViewById(R.id.bt_sign);
        progressBar = findViewById(R.id.progress_bar_sign);
        presenter = new SignPresenter(this);
        presenter.getSaying();
        presenter.getDays();
        presenter.isSign();
        ((TextView) findViewById(R.id.tv_title)).setText("每日一签");
        btSign.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_sign:
                presenter.sign();
                break;
            default:
                break;
        }
    }

    @Override
    public void showProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btSign.setEnabled(false);
                btSign.setText("");
                progressBar.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void hideProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressBar.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void setSaying(final String saying) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvSaying.setText(saying);
            }
        });
    }

    @Override
    public void setDays(final int days) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvDays.setText(days + "天");
            }
        });
    }

    @Override
    public void setSign(final boolean isSign) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isSign) {
                    btSign.setEnabled(false);
                    btSign.setBackground(getDrawable(R.drawable.button_solid_unable));
                    btSign.setTextColor(getResources().getColor(R.color.white));
                    btSign.setText("已签到");
                    progressBar.setVisibility(View.GONE);
                } else {
                    btSign.setBackground(getDrawable(R.drawable.button_stroke));
                    btSign.setTextColor(getResources().getColor(R.color.colorAccent));
                    btSign.setText("签到");
                    btSign.setEnabled(true);
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }
}
